﻿@using Newbe.BookmarkManager.Services.EventHubs

@code {

    [Inject]
    public NotificationService NotificationService { get; set; }

    [Inject]
    public IAfEventHub AfEventHub { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            AfEventHub.RegisterHandler<UserNotificationEvent>(HandleUserNotificationEvent);
            await AfEventHub.EnsureStartAsync();
        }
    }


    private async Task HandleUserNotificationEvent(UserNotificationEvent afEvent)
    {
        switch (afEvent.AfNotificationType)
        {
            case AfNotificationType.Info:
                await NotificationService.Info(new NotificationConfig
                {
                    Description = afEvent.Description,
                    Message = afEvent.Message
                });
                break;
            case AfNotificationType.Warning:
                await NotificationService.Warning(new NotificationConfig
                {
                    Description = afEvent.Description,
                    Message = afEvent.Message
                });
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

}